'''
- 给定以下列表, 通过**匿名函数**和**常规函数**根据键`age`来排序

  ```
  classes = [
      {"name": "abyss", "age": 25, "grade": "A"},
      {"name": "kin", "age": 23, "grade": "B"},
      {"name": "tian", "age": 24, "grade": "B"},
      {"name": "hun", "age": 26, "grade": "A"},
      {"name": "exe", "age": 21, "grade": "A"}
  ]
  ```

- 2021年马上就要到来了, 通过`map`函数将上述同学的`age`+1

  ```
  * 结合可变对象, 好好想一想
  ```

- 通过`filter`函数过滤掉上述同学中`grade`是`A`的同学

- 使用递归来重构斐波那契函数

'''

import pprint

classes = [
    {"name": "abyss", "age": 25, "grade": "A"},
    {"name": "kin", "age": 23, "grade": "B"},
    {"name": "tian", "age": 24, "grade": "B"},
    {"name": "hun", "age": 26, "grade": "A"},
    {"name": "exe", "age": 21, "grade": "A"}
]


def is_same_element(stu1: list, stu2: list):
    """
    判断两个'学生'列表中的元素是否一致
    """
    for ele in stu1:
        if ele not in stu2:
            return False
    return True


# (1) 根据匿名函数来排序
classes_1 = sorted(classes, key=lambda one: one['age'])
print(pprint.pformat(classes_1))
print(is_same_element(classes, classes_1))
print(classes is classes_1)


# (2) 根据常规函数来排序
def age_sort(student: dict):
    return student.get("age")


classes_2 = sorted(classes, key=age_sort)
print(pprint.pformat(classes_2))
print(is_same_element(classes, classes_2))
print(classes is classes_2)


# (3) 通过 map 将上述同学的 age+1
def add_age(student: dict):
    student['age'] += 1
    return student


classes_3 = list(map(add_age, classes))
print(pprint.pformat(classes_3))
print(is_same_element(classes, classes_3))
print(classes is classes_3)


# (4) 通过 filter 过滤掉上述同学中`grade`是`A`的同学
def is_grade_A(student: dict):
    return student.get("grade") == 'A'


classes_4 = list(filter(is_grade_A, classes))
print(pprint.pformat(classes_4))
print(is_same_element(classes, classes_4))
print(classes_4 is classes)
